import 'package:floor/floor.dart';
import 'package:share_fit/app/models/base_model.dart';
import 'package:share_fit/app/tools/date_tool.dart';

/// 运动效果，包含心率
@Entity(tableName: 'sports_effect')
class EffectModel extends BaseModel {
  /// 运动模块产生的运动记录id
  @ColumnInfo(name: 'sport_id')
  late int sportId;

  /// 运动时的心率, 单位：bpm
  @ColumnInfo(name: 'heart_rate')
  late int heartRate;

  /// 运动时的心率区间类型
  @ColumnInfo(name: 'heart_rate_zone')
  late int heartRateZone;

  /// 日期时间，格式 ：yyyy-MM-dd HH:mm
  @ColumnInfo(name: 'calendar_time')
  late String? calendarTime;

  /// 日期，格式 ：yyyyMMdd
  late String? calendar;

  @override
  bool from(Map map) {
    try {
      var heartRate1 = map['heartRate'];
      var heartRateZone1 = map['heartRateZone'];

      /// ios 没有heartRateZone字段，暂时只判断heartRate
      // if ((heartRate1 == null || heartRate1 == 0) && (heartRateZone1 == 0 || heartRateZone1 == null)) {
      if (heartRate1 == null) {
        return false;
      }

      heartRate = heartRate1;
      heartRateZone = heartRateZone1 ?? 0;
      if (map['timeInfo'] > 0) {
        DateTime dt = DateTool.getDateTimeFrom(map['timeInfo']);
        String s = DateTool.getYMDHMWithDash(dt);
        calendarTime = s;
        calendar = s.split(' ').first;
      }
      return true;
    } catch (e) {
      return false;
    }
  }

  @override
  toMap() {
    return {
      'sportId': sportId,
      'heartRate': heartRate,
      'heartRateZone': heartRateZone,
      'calendarTime': calendarTime,
      'calendar': calendar,
    };
  }

  @override
  List<Object?> get props => [heartRate, heartRateZone, calendarTime, calendar];

  @override
  bool? get stringify => true;
}
